package com.ysqcould.message.service.impl;


import com.ysqcould.allocation.mapper.MessageConfigMapper;
import com.ysqcould.common.utils.DateUtils;
import com.ysqcould.message.mapper.WxMessagePressMapper;
import com.ysqcould.message.domain.WxMessage;
import com.ysqcould.message.domain.vo.WxMessageVo;
import com.ysqcould.message.mapper.WxMessageMapper;
import com.ysqcould.message.service.IWxMessageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统消息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-06-30
 */
@Service
public class WxMessageServiceImpl implements IWxMessageService
{
    @Resource
    private WxMessageMapper wxMessageMapper;

    @Resource
    private MessageConfigMapper configMapper;

    @Resource
    private WxMessagePressMapper messagePressMapper;

    /**
     * 查询系统消息
     * 
     * @param id 系统消息主键
     * @return 系统消息
     */
    @Override
    public WxMessage selectWxMessageById(Long id)
    {
        return wxMessageMapper.selectWxMessageById(id);
    }

    /**
     * 查询系统消息列表
     * 
     * @param wxMessage 系统消息
     * @return 系统消息
     */
    @Override
    public List<WxMessageVo> selectWxMessageList(WxMessage wxMessage)
    {
        final List<WxMessageVo> list = wxMessageMapper.selectWxMessageList(wxMessage);
        list.forEach(p->{
            if (p.getTemplateType().equals("1")){
                final String[] split = p.getKeyword2Data().split("\\\\n");
                final int length = split.length;
                if (length>1){
                    p.setKeyword2Data(split[0]+"-"+split[1]);
                }else {
                    p.setKeyword2Data(split[0].replace("\\\\n",""));
                }
            }
            if (p.getTemplateType().equals("5")){
                final String[] split = p.getKeyword3Data().split("\\\\n");
                if (split.length>1){
                    p.setKeyword3Data(split[0]+"-"+split[1]);
                }else {
                    p.setKeyword3Data(split[0]);
                }
            }
        });

        return list;
    }

    /**
     * 新增系统消息
     * 
     * @param wxMessage 系统消息
     * @return 结果
     */
    @Override
    public int insertWxMessage(WxMessage wxMessage)
    {
        wxMessage.setCreateTime(DateUtils.getNowDate());
        return wxMessageMapper.insertWxMessage(wxMessage);
    }

    /**
     * 修改系统消息
     * 
     * @param wxMessage 系统消息
     * @return 结果
     */
    @Override
    public int updateWxMessage(WxMessage wxMessage)
    {
        wxMessage.setUpdateTime(DateUtils.getNowDate());
        return wxMessageMapper.updateWxMessage(wxMessage);
    }

    /**
     * 批量删除系统消息
     * 
     * @param ids 需要删除的系统消息主键
     * @return 结果
     */
    @Override
    public int deleteWxMessageByIds(Long[] ids)
    {
        return wxMessageMapper.deleteWxMessageByIds(ids);
    }

    /**
     * 删除系统消息信息
     * 
     * @param id 系统消息主键
     * @return 结果
     */
    @Override
    public int deleteWxMessageById(Long id)
    {
        return wxMessageMapper.deleteWxMessageById(id);
    }
}
